#######################################
# Plotting Code Political Outcome - Crossing Borders Project
#######################################

#Make sure to load 03analysis_v5.R before starting this code. Comment the following out if not done already
#source("03analysis_v5.R")

library(ggplot2)
library(viridis)
library(tidyverse)
library(patchwork)
library(extrafont)
library(ggthemr) # devtools::install_github('cttobin/ggthemr')
ggthemr("light", layout = "plain")

library(cowplot) # version 1.1.1 # devtools::install_github("wilkelab/cowplot")


######################
# 1. First-stage effects plot for paper
# Thre panel design:
# Top-Left: raw data with smoothers
# Top-Right: Autor effects
# Bottom: D-i-D estimates
######################

cb[ border15 == T , TR_three := "0-15 Minutes"]
cb[ border30 == T, TR_three := "15-30 Minutes"]

# Raw voting data over time -------------------------------------
fs_raw <- cb %>% filter(year >= 1991 & travelMUNmin <= 30) %>% 
  ggplot(aes(x = year, y = frPrct, color = TR_three, fill = TR_three, linetype = TR_three, shape = TR_three)) + 
  stat_summary(fun.y = mean, geom = "point") + 
  geom_smooth(method = "loess") + 
  geom_vline(xintercept = c(1999, 2004), linetype = "dashed", color = "black") +
  ylab("Anti-Immigrant Party Vote Share") +
  xlab("Year") +
  theme_minimal() + 
  scale_x_continuous(breaks = seq(1991, 2019, 2), expand = expansion(mult = 0, add = 0.5)) +
  scale_color_manual(values = cbPalette[-3], name = NULL, guide = "legend") +
  scale_fill_manual(values = cbPalette[-3], name = NULL, guide = "legend") +
  scale_linetype_manual(values = c("solid", "dashed", "dotted"), name = NULL, guide = "legend") +
  scale_shape_manual(values = c(16, 17, 18), name = NULL, guide = "legend") +
  scale_y_continuous(breaks = seq(20, 35, 2)) +
  annotate("text", x = 1994.5, y = 36, label = "Pre-Reform", hjust = 0.5) +
  annotate("text", x = 2001.5, y = 36, label = "Transition", hjust = 0.5) +
  annotate("text", x = 2009.75, y = 36, label = "Free Movement", hjust = 0.5) +
  theme(legend.position = "bottom",
        legend.background = element_rect(color = "black"),
        legend.margin = margin(4,4,4,4,unit = "pt"),
        axis.title.y = element_text(vjust = 0)) +
  ggtitle("A. Anti-Immigrant Party Support in Border Municipalities")




# Event study plot -------------------------------------
event_study_dat <- data.frame(
  y = coef(mod_pol_es_weights),
  ymin = coef(mod_pol_es_weights) - qt(.975, df = df.residual(mod_pol_es_weights))*sqrt(diag(vcov(mod_pol_es_weights))),
  ymax = coef(mod_pol_es_weights) + qt(.975, df = df.residual(mod_pol_es_weights))*sqrt(diag(vcov(mod_pol_es_weights))),
  Year = as.numeric(gsub("border[[:digit:]][[:digit:]]_", "", names(coefficients(mod_pol_es_weights))))
)

event_study_dat <- rbind(setDT(event_study_dat), data.table(c(0), c(0), c(0), c(1999)), use.names = F)

fs_event <- ggplot(data = event_study_dat, aes(x = Year, y = y, ymin = ymin, ymax = ymax, 
                                               color = factor(ifelse(Year <= 1999, 3, ifelse(Year < 2004, 2, 1))),
                                               shape = factor(ifelse(Year <= 1999, 3, ifelse(Year < 2004, 2, 1)))
                                               )) + 
  geom_vline(xintercept = c(1999, 2004), linetype = 2, color = "black") +
  geom_hline(yintercept = 0, linetype = 2, color = "black") +
  geom_pointrange() +
  annotate("text", x = 1994.5, y = 13, label = "Pre-Reform", hjust = 0.5) +
  annotate("text", x = 2001.5, y = 13, label = "Transition", hjust = 0.5) +
  annotate("text", x = 2009.75, y = 13, label = "Free Movement", hjust = 0.5) +
  scale_color_manual(values = cbPalette[c(1,2,8)], name = NULL, labels = c("Free Movement", "Transition", "Pre-Reform"), guide = guide_legend(reverse = T)) +
  scale_shape_manual(values = c(16, 17, 18), 
                     name = NULL, 
                     labels = c("Free Movement", "Transition", "Pre-Reform"), 
                     guide = guide_legend(reverse = T)) + 
  ylab("Change in Voting for Anti-Immigrant Parties") +
  theme_minimal() + 
  scale_x_continuous(breaks = seq(1991, 2019,2)) +
  theme(legend.position = "bottom",
        legend.background = element_rect(color = "black"),
        legend.margin = margin(4,4,4,4,unit = "pt")) +
  ggtitle("B. Effect of Border Proximity By Election")


# DiD plot -------------------------------------
fs_did <- ggplot(coef.gg.prep(list(mod_pol_weights)), aes(x = modelcoef, y = -1*y, xmin = ylo95, xmax = yhi95, color = variable, shape = variable, group = variable)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  ggstance::geom_linerangeh(position = ggstance::position_dodgev(.25)) +
  ggstance::geom_pointrangeh(aes(xmin = ylo90, xmax = yhi90), size = 1.25, position = ggstance::position_dodgev(.25)) +
  ylab(NULL) +
  scale_y_continuous(breaks = c(-2, -1), labels = c("Transition\nPeriod", "Free\nMovement"), expand = expand_scale(mult = 0, add = 0.5)) +
  scale_color_manual(name = NULL, guide = "none", values = cbPalette[-3]) +
  scale_shape_discrete(name = NULL, guide = "none") +
  xlab("Effect of Border Proximity on Anti-Immigrant Party Support") +
  ggtitle("C. Difference-in-Differences Estimates") +
  theme_bw() + 
  theme(legend.position = "bottom",
        legend.background = element_rect(color = "black"),
        legend.margin = margin(4,4,4,4,unit = "pt"))

# Combined plot -------------------------------------
plots <- align_patches(fs_raw, fs_did)
top_row <- plot_grid(plots[[1]], fs_event, align = "h")
pdf(here("figures", "Fig3.pdf"), height = 8.27, width = 11.69)
plot_grid(top_row, plots[[2]], ncol = 1, rel_heights = c(3, 1.5))
dev.off()
